<template>
  <van-nav-bar
    title="地址"
    left-arrow
    @click-left="onClickLeft"
  />
  <van-address-list
    v-model="chosenAddressId"
    :list="list"
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
    @select="defaultAdd"
  />
</template>

<script setup>
import { ref } from 'vue';
import { getUserAddApi } from '@/server';
import { useRouter } from 'vue-router';
const router=useRouter()
// 返回
const onClickLeft = () => history.back();
// 选择框
const chosenAddressId = ref();
// 地址列表
const list = ref([])
// 获取自己的地址列表
const getUserAddList=async ()=>{
  let res=await getUserAddApi()
  res.data.result.forEach(ele => {
    let data={}
    data.id=ele.id
    data.name=ele.name
    data.area=ele.area
    data.tel=ele.phone
    data.desc=ele.desc
    data.default_set=ele.default_set
    data.area_name=ele.area_name
    data.address=ele.area_name+ele.desc
    list.value.push(data)
    if(ele.default_set==='1'){
      chosenAddressId.value=ele.id
    }
  });
}
getUserAddList()
// 新增按钮
const onAdd = () => {
  router.push({path:'/AddressEdit',
  query:{name:'add'}})
};
// 编辑按钮
const onEdit = (item) =>{
  router.push({path:'/AddressEdit',
  query:{name:'edit',data:JSON.stringify(item)}})
};
// 选中按钮函数
const defaultAdd=(item,index)=>{
  const defAddress=item
  localStorage.setItem('defAddress',JSON.stringify(defAddress))
}
defaultAdd()
</script>

<style lang='scss'>

</style>
